﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Drawing;

namespace Link
{
    /// <summary>
    /// 用于路径寻找的类
    /// </summary>
    class PathOld
    {
        public PathOld()
        {
        }
        #region user function
        /// <summary>
        /// 判断两游戏块是否能消除
        /// </summary>
        /// <param name="preIndex"></param>
        /// <param name="curIndex"></param>
        /// <returns></returns>
        public bool IsAdjacent(int preIndex, int curIndex)
        {
            bool result = false;
            int preClick = colNum + (preIndex - 1) / (colNum - 2) * 2 + preIndex;
            int curClick = colNum + (curIndex - 1) / (colNum - 2) * 2 + curIndex;
            Point pre = new Point(preClick % colNum, preClick / colNum);
            Point cur = new Point(curClick % colNum, curClick / colNum);
            if (pre.X == cur.X || pre.Y == cur.Y)
            {
                if (IsLine(pre, cur))
                    result = true;
                else
                    result = IsTwoCross(pre, cur);
            }
            else
            {
                if (IsOneCross(pre, cur))
                    result = true;
                else
                    result = IsTwoCross(pre, cur);
            }
            if (result)
            {
                status[pre.X, pre.Y] = 0;
                status[cur.X, cur.Y] = 0;
            }
            return result;
        }
        /// <summary>
        /// 测试两点是否能直接相连，要求两点在同一直线上
        /// </summary>
        /// <param name="pre"></param>
        /// <param name="cur"></param>
        /// <returns></returns>
        private bool IsLine(Point pre, Point cur)
        {
            int max,min;
            //垂直方向
            if(pre.X == cur.X)
            {
                max = pre.Y > cur.Y ? pre.Y : cur.Y;
                min = pre.Y < cur.Y ? pre.Y : cur.Y; 
                for(int i = min + 1; i < max; i++)
                {
                    if(status[pre.X,i] > 0)
                        return false;
                }
            }
            //水平方向
            else
            {
                max = pre.X > cur.X ? pre.X : cur.X;
                min = pre.X < cur.X ? pre.X : cur.X; 
                for(int i = min + 1; i < max; i++)
                {
                    if(status[i,pre.Y] > 0)
                        return false;
                }
            }
            return true;
        }
        /// <summary>
        /// 测试两点是否通过一个拐角相连
        /// </summary>
        /// <param name="pre"></param>
        /// <param name="cur"></param>
        /// <returns></returns>
        private bool IsOneCross(Point pre, Point cur)
        {
            Point c1 = new Point(pre.X, cur.Y);
            Point c2 = new Point(cur.X, pre.Y);
            if(status[c1.X, c1.Y] <= 0)
            {
                if (IsLine(pre, c1) && IsLine(c1, cur))
                    return true;
            }
            if(status[c2.X, c2.Y] <= 0)
            {
                if (IsLine(pre, c2) && IsLine(c2, cur))
                    return true;
            }
            return false;
        }
        /// <summary>
        /// 测试两点是否通过两个拐角相连
        /// </summary>
        /// <param name="pre"></param>
        /// <param name="cur"></param>
        /// <returns></returns>
        private bool IsTwoCross(Point pre, Point cur)
        {
            int min, max;
            if (pre.X == cur.X && pre.Y != cur.Y)
            {
                int[] array = new int[colNum - 1];
                if (pre.X <= colNum - pre.X - 1)
                {
                    min = pre.X;
                    max = colNum - pre.X - 1;
                    for (int i = 0; i < min; i++)
                    {
                        array[2 * i] = -(i + 1);
                        array[2 * i + 1] = i + 1;
                    }
                    for (int i = min; i < max; i++)
                    {
                        array[min + i] = i + 1;
                    }
                }
                else
                {
                    min = colNum - pre.X - 1;
                    max = pre.X;
                    for (int i = 0; i < min; i++)
                    {
                        array[2 * i] = -(i + 1);
                        array[2 * i + 1] = i + 1;
                    }
                    for (int i = min; i < max; i++)
                    {
                        array[min + i] = -(i + 1);
                    }
                }
                Point p;
                for (int i = 0; i < array.Length; i++)
                {
                    p = new Point(pre.X + array[i], pre.Y);
                    if (status[p.X,p.Y] <= 0 && IsLine(pre, p) && IsOneCross(p, cur))
                    {
                        return true;
                    }
                }
                return false;
            }
            else if (pre.Y == cur.Y && pre.X != cur.X)
            {
                int rowNum = status.Length / colNum;
                int[] array = new int[rowNum - 1];
                if (pre.Y <= rowNum - pre.Y - 1)
                {
                    min = pre.Y;
                    max = rowNum - pre.Y - 1;
                    for (int i = 0; i < min; i++)
                    {
                        array[2 * i] = -(i + 1);
                        array[2 * i + 1] = i + 1;
                    }
                    for (int i = min; i < max; i++)
                    {
                        array[min + i] = i + 1;
                    }
                }
                else
                {
                    min = rowNum - pre.Y - 1;
                    max = pre.Y;
                    for (int i = 0; i < min; i++)
                    {
                        array[2 * i] = -(i + 1);
                        array[2 * i + 1] = i + 1;
                    }
                    for (int i = min; i < max; i++)
                    {
                        array[min + i] = -(i + 1);
                    }
                }
                Point p;
                for (int i = 0; i < array.Length; i++)
                {
                    p = new Point(pre.X, pre.Y + array[i]);
                    if (status[p.X,p.Y] <= 0 && IsLine(pre, p) && IsOneCross(p, cur))
                    {
                        return true;
                    }
                }
                return false;
            }
            //不能找到最短路径
            else
            {
                //水平线上竖直扫描
                Point p;
                for (int i = 0; i < colNum; i++)
                {
                    p = new Point(i, pre.Y);
                    if (status[p.X, p.Y] <= 0 && IsLine(pre, p) && IsOneCross(p, cur))
                    {
                        return true;
                    }
                }
                //竖直线上的水平扫描
                int rowNum = status.Length / colNum;
                for (int i = 0; i < rowNum; i++)
                {
                    p = new Point(pre.X, i);
                    if (status[p.X, p.Y] <= 0 && IsLine(pre, p) && IsOneCross(p, cur))
                    {
                        return true;
                    }
                }
                return false;
            }
        }
        #endregion
        #region field and propety
        private int[,] status;
        /// <summary>
        /// 状态数组
        /// </summary>
        public int[,] Status
        {
            get { return status; }
            set { status = value; }
        }
        private int colNum;
        /// <summary>
        /// 一维元素的个数
        /// </summary>
        public int ColNum
        {
            get { return colNum; }
            set { colNum = value; }
        }
        //private List<int> points;
        ///// <summary>
        ///// 路径上的块索引号列表
        ///// </summary>
        //public List<int> Points
        //{
        //  get { return points; }
        //  set { points = value; }
        //}

        #endregion
    }
}
